import hilog from '@ohos.hilog';
import { HttpInterceptor, RequestChain, ResponseChain } from './RequestOptions';

export class LoggingInterceptor implements HttpInterceptor {
  private domain: number = 0x0001;
  private prefix: string = 'HttpClient';
  private format: string = '%{public}s';

  async onRequest(chain: RequestChain): Promise<RequestChain> {
    hilog.error(this.domain, this.prefix, this.format,
      `Request: ${chain.method} ${chain.url}`);
    if (chain.header) {
      hilog.error(this.domain, this.prefix, this.format,
        `Request Header: ${JSON.stringify(chain.header)}`);
    }
    if (chain.data) {
      hilog.error(this.domain, this.prefix, this.format,
        `Request Body: ${JSON.stringify(chain.data)}`);
    }

    return chain;
  }

  async onResponse(chain: ResponseChain): Promise<ResponseChain> {
    hilog.error(this.domain, this.prefix, this.format,
      `Response: ${chain.method} ${chain.response.responseCode} ${chain.url}`);
    hilog.debug(this.domain, this.prefix, this.format,
      `Response Header: ${JSON.stringify(chain.response.header)}`);
    hilog.debug(this.domain, this.prefix, this.format,
      `Response Body: ${JSON.stringify(chain.response)}`);
    return chain;
  }
} 